package dataview;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Data extends GregorianCalendar {
	private static final long serialVersionUID = -4491064597710386837L;

	@Override
	public int hashCode() {
		return super.hashCode();
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj) return true;
		if (!(obj instanceof Data)) return false;
		final Data o = (Data)obj;
		return (get(Calendar.YEAR) == o.get(Calendar.YEAR) && get(Calendar.MONTH) == o.get(Calendar.MONTH) && get(Calendar.DATE) == o.get(Calendar.DATE));
	}
	
	public Data() {
		super();
	}
	
	public Data(int gg, int mm, int aa) {
		super(aa, mm - 1, gg);
	}
	
	public Data(Date date) {
		super();
		setTime(date);
	}
	
	public Data(String xmlData) {
		super();
		if (xmlData == null) throw new IllegalArgumentException();
		String[] string = xmlData.split("-");
		if (string.length != 3) throw new IllegalArgumentException();
		set(Integer.parseInt(string[0]), Integer.parseInt(string[1]) - 1, Integer.parseInt(string[2]));
	}
	
	public String toXMLFormat() {
		return int2String(get(YEAR), 4) + "-" + int2String(get(MONTH) + 1, 2) + "-" + int2String(get(DAY_OF_MONTH), 2);
	}
	
	public static String toReportFormat() {
		Data now = new Data();
		return now.int2String(now.get(YEAR), 4) + now.int2String(now.get(MONTH) + 1, 2) + now.int2String(now.get(DAY_OF_MONTH), 2) + "_" + now.int2String(now.get(HOUR_OF_DAY), 2) + now.int2String(now.get(MINUTE), 2) + now.int2String(now.get(SECOND), 2);
	}
	
	private String int2String(int numero, int lunghezza) {
		String s = Integer.toString(numero);
		while (s.length() != lunghezza)
			if (s.length() < lunghezza)
				s = "0" + s;
			else s = s.substring(0, lunghezza);
		return s;
	}
}
